package org.zfes.snowier.common.monitor;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.zfes.snowier.common.monitor.dto.ClassLoadDto;
import org.zfes.snowier.common.monitor.dto.GcDto;
import org.zfes.snowier.common.monitor.dto.JstackDto;
import org.zfes.snowier.common.monitor.dto.KVDto;
import org.zfes.snowier.common.monitor.dto.ThreadDto;
import org.zfes.snowier.common.monitor.jvm.Jstack;
import org.zfes.snowier.common.monitor.jvm.Jstat;
import org.zfes.snowier.common.monitor.jvm.Server;


public class Acquisitor {
	  public static String now(){
	        SimpleDateFormat format = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
	        return format.format(new Date());
	 }


	    
	public static String  getPid(){
	        String name = ManagementFactory.getRuntimeMXBean().getName();
	        String pid = name.split("@")[0];
	        return  pid;
	}
	    
	public static Optional<Server> getInstanceInfo(String instanceKey){
	    	  Server server = new Server();
	    	try {
				server.copyTo();
				server.setRunningInstance(instanceKey);
				return Optional.ofNullable(server);
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	return Optional.empty();
	    }
	public static void main(String[] args) throws Exception {
		 Server server = new Server();
		 server.copyTo();
		 System.out.println(server);
	}
	    public static Optional<GcDto> getGc(String instanceKey){
		try {
				List<KVDto> kvEntities = Jstat.jstatGc();
			
		         GcDto entity = new GcDto();
		         entity.setId(Integer.valueOf(getPid()));
		         entity.setName(instanceKey);
		         entity.setDate(now());
		         entity.setS0C(kvEntities.get(0).getValue());
		         entity.setS1C(kvEntities.get(1).getValue());
		         entity.setS0U(kvEntities.get(2).getValue());
		         entity.setS1U(kvEntities.get(3).getValue());
		         entity.setEC(kvEntities.get(4).getValue());
		         entity.setEU(kvEntities.get(5).getValue());
		         entity.setOC(kvEntities.get(6).getValue());
		         entity.setOU(kvEntities.get(7).getValue());
		         entity.setMC(kvEntities.get(8).getValue());
		         entity.setMU(kvEntities.get(9).getValue());
		         entity.setCCSC(kvEntities.get(10).getValue());
		         entity.setCCSU(kvEntities.get(11).getValue());
		         entity.setYGC(kvEntities.get(12).getValue());
		         entity.setYGCT(kvEntities.get(13).getValue());
		         entity.setFGC(kvEntities.get(14).getValue());
		         entity.setFGCT(kvEntities.get(15).getValue());
		         entity.setGCT(kvEntities.get(16).getValue());
		     	return Optional.ofNullable(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
		return Optional.empty();
	}

	    public static Optional<ThreadDto> getThread(String instanceKey){
	    	
	          try {
	              JstackDto info = Jstack.jstack();
	              ThreadDto entity = new ThreadDto();
	              entity.setId(Integer.valueOf(info.getId()));
	              entity.setName(instanceKey);
	              entity.setDate(now());
	              entity.setTotal(info.getTotal());
	              entity.setRUNNABLE(info.getRUNNABLE());
	              entity.setTIMED_WAITING(info.getTIMED_WAITING());
	              entity.setWAITING(info.getWAITING());
	              return Optional.ofNullable(entity);
	          }catch (Exception e){
	              e.printStackTrace();
	          }
	          return Optional.empty();
	    }

	    public static  Optional<ClassLoadDto> getClassload(String instanceKey){
	    	
			try {
				List<KVDto> jstatClass=Jstat.jstatClass();
				
		        ClassLoadDto entity = new ClassLoadDto();
		        entity.setId(Integer.valueOf(getPid()));
		        entity.setName(instanceKey);
		        entity.setDate(now());
		        entity.setLoaded(jstatClass.get(0).getValue());
		        entity.setBytes1(jstatClass.get(1).getValue());
		        entity.setUnloaded(jstatClass.get(2).getValue());
		        entity.setBytes2(jstatClass.get(3).getValue());
		        entity.setTime1(jstatClass.get(4).getValue());
		        entity.setCompiled(jstatClass.get(5).getValue());
		        entity.setFailed(jstatClass.get(6).getValue());
		        entity.setInvalid(jstatClass.get(7).getValue());
		        entity.setTime2(jstatClass.get(8).getValue());
		        
		        return Optional.ofNullable(entity);
		        
			} catch (Exception e) {
				e.printStackTrace();
			}
			 return Optional.empty();
	    }

}
